/**
 * Copyright (c) 2015 www.smarthome.com All rights reserved. <br />
 * 本软件源代码版权归xxxx公司,未经许可不得任意复制与传播.<br />
 *
 */
package com.smarthome.core.kuaidiXiyi.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShAccountService;
import com.smarthome.base.service.ShHistoryAccountService;
import com.smarthome.base.service.ShOrdersService;
import com.smarthome.core.kuaidiXiyi.RecevieCourierService;
import com.smarthome.core.message.util.MessageUtil;
import com.smarthome.core.messageTask.MessageTaskService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShOrders;
import com.smarthome.domain.enums.OrdersPaymentStatus;
import com.smarthome.domain.enums.PushMobileType;
import com.smarthome.domain.enums.SmsTypeEnum;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.TaskMessageVo;
import com.smarthome.rpc.PushMobileMessageService;

/**
 * 
* @ClassName: SendCourierServiceImpl
* @Description: 发快递service接口实现类
* @author cyj
* @date 2016年3月24日 上午10:50:10
*
 */
@Service("recevieCourierService")
public class RecevieCourierServiceImpl implements RecevieCourierService, MessageTaskService{


    @Resource
    private ShOrdersService shOrdersOfSendCourierService;
    
    
    @Resource
    private ShHistoryAccountService shHistoryAccountService;
    
    @Resource
    private ShAccountService shAccountService;

    @Resource
	private PushMobileMessageService pushMobileMessageService;
    
    @Override
    public DataGrid dataGrid(ShOrders shOrders, PageHelper page) {
        return shOrdersOfSendCourierService.queryShOrdersPage(shOrders, page);
    }




	@Override
	public ShOrders getDetailById(Long id) {
		return shOrdersOfSendCourierService.getDetailById(id);
	}





	@Override
	public Integer add(ShOrders shOrders) {
		Date nowDate = new Date();
		
		
		shOrders.setGmtCreate(nowDate);
		shOrders.setGmtModify(nowDate);
		shOrders.setOrderType(1);//待收快递
		shOrders.setStatus(1);//待取状态
		shOrders.setPaymentStatus(OrdersPaymentStatus.DEEPEND.getType());//未支付
		int result = 0;
		try{
			result = shOrdersOfSendCourierService.insert(shOrders);
		}catch(Exception e) {
			e.printStackTrace();
			return 0;
		}
		return result;
	}




	@Override
	public Integer edit(ShOrders shOrders) {
		Date nowDate = new Date();
		ShOrders orders = shOrdersOfSendCourierService.selectEntryById(shOrders.getId());
		BeanUtils.copyProperties(shOrders, orders);
		orders.setGmtModify(nowDate);
		
		int result = 0;
		try{
			result = shOrdersOfSendCourierService.saveOrUpdate(orders);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}




	@Override
	public Integer chengeStatus(Long id,Integer status) {
		ShOrders shOrders = shOrdersOfSendCourierService.selectEntryById(id);
		shOrders.setStatus(status);
		return shOrdersOfSendCourierService.saveOrUpdate(shOrders);
	}


	@Override
	public List<ShOrders> getRecevieCourierDetailList(ShOrders shOrders) {
		return shOrdersOfSendCourierService.getSendCourierDetailList(shOrders);
	}


	@Override
	public TaskMessageVo getTaskMessage(Long residenceId) {
		TaskMessageVo vo = new TaskMessageVo();
		
		vo.setMessageType(TaskMessageVo.TaskMessageType.RECEVIECOURIER);
		ShOrders shOrders = new ShOrders();
		shOrders.setResidenceId(residenceId);
		shOrders.setStatus(1);//待取状态
		shOrders.setOrderType(1);//待收快递
		vo.setCount(shOrdersOfSendCourierService.getSendCourierCount(shOrders).longValue());
		vo.setParam("status=1");
		return vo;
	}




	@Override
	public boolean sendMsg(ShOrders shOrders) {
    	String mobile = shOrders.getDesMobile();
    	ShAccount account = new ShAccount();
    	account.setMobile(mobile);
    	List<String> ids = shAccountService.getShAccountIds(account);
    	if(null != ids && ids.size() > 0){
    		pushMobileMessageService.pushMsgToSome("您有一个待取快递，请尽快收取。", PushMobileType.ACCOUNTMESSAGE.getCode(), ids.toArray(new String[ids.size()]));
    	}
    	String code = shOrders.getRemark() == null?"":shOrders.getRemark();
    	MessageUtil.sendTimelyForTemplet(SmsTypeEnum.RECEIVE_KD,shOrders.getResidenceId(), mobile, code);
		return false;
	}
	
	
}
